#!/usr/bin/env python
#coding:utf-8

import sys
import datetime
import json
#import comm.common_tools as commtools

# 盗用榜单文件中的定义
eSongBaseInfoField_SKIP = 1
eSongBaseInfoField_AuthorID = 0
eSongBaseInfoField_AuthorName = 1
eSongBaseInfoField_GlobalID = 2
eSongBaseInfoField_Title = 3
eSongBaseInfoField_AlbumID = 12
eSongBaseInfoField_proxycompany = 14
eSongBaseInfoField_Area = 17
eSongBaseInfoField_PublishTime = 19
eSongBaseInfoField_DelStatus = 27
eSongBaseInfoField_Distribution = 29
eSongBaseInfoField_ResourceProvider = 34
eSongBaseInfoField_TingUID = 46
eSongBaseInfoField_MAX = 1 + eSongBaseInfoField_TingUID + eSongBaseInfoField_SKIP


# 处理一首歌曲。只保留在线歌曲并且多端分发状态PCweb可听的歌曲
def process_song(line):
	item = line.split('\t', eSongBaseInfoField_MAX)
	if len(item) < eSongBaseInfoField_MAX:
		return False
	
	for i in range(0, eSongBaseInfoField_SKIP):
		item.pop(0)
		
	if item[eSongBaseInfoField_DelStatus] != '0':   # 在线歌曲
		return False
	
	distribution = json.loads(item[eSongBaseInfoField_Distribution])
	if distribution['0'][0] != '0': # 多端分发状态PCweb可听的歌曲
		return False
	
	return True


def process_song_file(file_in, file_out):

	fout = open(file_out, 'w')  # 若是'wb'就表示写二进制文件
	
	stat = [0] * 3  # 统计
	print 'START:', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
	with open(file_in, 'rb') as fin:
		for line in fin:
			stat[0] += 1
			
			if process_song(line):
				fout.write(line)
				stat[1] += 1
			else:
				stat[2] += 1
			
			if stat[0] % 1000000 == 0:
				print 'RUN:  ', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), stat
	
	print 'END:  ', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), stat


#   logging.info('========STARTUP pid=[%d] cmd=%s ========' % (os.getpid(), sys.argv))
def main():
#	commtools.init_log()

	file_in = '/home/liangquan/data/quku/data/released/song.txt'
	file_out = 'song.small.txt'
	process_song_file(file_in, file_out)
#	process_song_file(sys.argv[1], sys.argv[2])


if __name__ == "__main__":
	'''
	argc = len(sys.argv)
	if 2 > argc or ('-v' == sys.argv[1] or '-h' == sys.argv[1]):
		print '\n\t command line is like \'python %s file\'\n' % __file__
	else:
		main()
	'''
	main()


'''
	LiangQuan, 2019/02/13,
	对song.txt文件进行预处理，剔除DelStatus!=0的和Distribution!=0的歌曲，降低ranktool的内存开销
	
'''
